In [1]:
import os
import folium
import json
print(folium.__version__)
zoom_start's bigger value means zoom in folium.Map(location=[x,x]) creates a map centred on x,x
In [2]:
gungahlin = folium.Map(location=[-35.187746,149.1201253],zoom_start=11)
popup is a small flag on the map
In [3]:
home_coordinate=[-35.1599026,149.0961]
home_marker=folium.Marker(home_coordinate,popup="jun's house")
home_marker.add_to(gungahlin)
Out[3]:
In [4]:
gungahlin
Out[4]:
In [5]:
geojson_file='./data/canberrageo.json'
geojson_layer = folium.GeoJson(open(geojson_file), name='geojson')
geojson_layer.add_to(gungahlin)
Out[5]:
canberrageo.json是关于堪培拉的地理信息geojson文件,它是featurecollection,以下是它的部分信息 { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "name": "Acton", "cartodb_id": 0, "created_at": "2013-11-26", "updated_at": "2013-11-26" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 149.098343, -35.291985 ], [ 149.098316, -35.291929 ], [ 149.098287, -35.291862 ], [ 149.098298, -35.291791 ], ........ 从这里可以发现,它就是把各个区的边界坐标收集起来放在geometry属性里
In [6]:
gungahlin
Out[6]:
In [7]:
path='./data/canberra_school.json'
json是方便于网络传输的编码 解码json,即load(xxx)就是将json文件提取出来弄成字典 可以同json_dict.keys()来知道该字典的关键词 一层一层剥洋葱,json就是字典套字典 底下这个函数提取json中关于各个学校的经度纬度
In [8]:
def parse_json(path):
fp=open(path)
json_dict=json.load(fp)
datas=json_dict['data']
for data in datas:
[x,y]=data[-1][1:3]
if type(x) is str:
x=float(x)
y=float(y)
yield [x,y]
fp.close()
In [9]:
school_geos=list(parse_json(path))
In [10]:
school_geos
Out[10]:
In [11]:
for item in school_geos:
school_marker=folium.Marker(item,popup="school")
school_marker.add_to(gungahlin)
以下这个运用cluster,地图显得非常简洁
In [13]:
gungahlin_cluster = folium.Map(location=[-35.187746,149.1201253],zoom_start=11)
marker_cluster = folium.MarkerCluster().add_to(gungahlin_cluster)
for item in school_geos:
school_marker=folium.Marker(item,popup="school")
school_marker.add_to(marker_cluster)
gungahlin_cluster
Out[13]:
In [ ]: